<?php

namespace App\Listeners;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use App\Events\Event;
use App\Events\UserLogin;
use App\Events\UserLogout;

class UserEventSubscriber
{

    public function onUserLogin(UserLogin $event)
    {
        $user = $event->user;
        Log::info("User $user->username Login.");
    }

    public function onUserLogout(UserLogout $event)
    {
        $user = $event->user;
        Log::info("User $user->username Logout.");
    }

    public function onUserEvent(Event $event)
    {
        $eventName = $event->getEventName();
        switch ($eventName) {
            case UserLogin::$eventName:
            case UserLogout::$eventName:
                Log::info("$eventName emit.");
                break;
        }
    }

    /**
     * 为订阅者注册监听器。
     *
     * @param  Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            UserLogin::class,
            UserEventSubscriber::class . '@onUserLogin'
        );

        $events->listen(
            UserLogout::class,
            UserEventSubscriber::class . '@onUserLogout'
        );

        $events->listen(
            [
                UserLogin::class,
                UserLogout::class,
            ],
            UserEventSubscriber::class . '@onUserEvent'
        );
    }
}
